package qianfeng.com.meijiadaka.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import qianfeng.com.meijiadaka.R;
import qianfeng.com.meijiadaka.bean.Bean;

/**
 * Created by Think on 2017/2/7.
 */

public class ShopTimedPromotionsRecyclerAdapter extends RecyclerView.Adapter<ShopTimedPromotionsRecyclerAdapter.MyViewHolder> {
    private List<Object> list;
    private Bean bean;
    private Context context;
    OnItemClickListener mListener;
    private RecyclerView rv;


    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        rv = recyclerView;
    }

    public interface OnItemClickListener{
        void onClick(int index);
    }
    public void setOnItemClickListener(OnItemClickListener itemClickListener) {
        this.mListener=itemClickListener;
    }


    public ShopTimedPromotionsRecyclerAdapter(Bean bean, Context context) {
        this.bean = bean;
        this.context = context;
        list = new ArrayList<>();
        list.addAll(bean.getResult().getSaleCommodities().getCommodities());
        Log.e( "ShopTimedPromotionsRecyclerAdapter: ",list.size()+"" );
    }

    //重写onCreateViewHolder方法，返回一个自定义的ViewHolder
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        final View view = LayoutInflater.from(context).inflate(R.layout.shop_timed_promotions_item,parent,false);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mListener!=null){
                    mListener.onClick(rv.getChildAdapterPosition(view));
                }
            }
        });
        return new MyViewHolder(view);
    }


    //填充onCreateViewHolder方法返回的holder中的控件
    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {

        int originalPrice = bean.getResult().getSaleCommodities().getCommodities().get(position).getSalePrice();
        int d=originalPrice/100;
        int originalPrice1 = bean.getResult().getSaleCommodities().getCommodities().get(position).getOriginalPrice();
        int d1=originalPrice1/100;

        holder.tvNew.setText(d+".00");
        holder.tvOld.setText(d1+".00");
        holder.tvOld.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG); //中划线
        holder.tvOld.getPaint().setAntiAlias(true);//抗锯齿

        Picasso.with(context).load(bean.getResult().getSaleCommodities().getCommodities().get(position).getCommodityIcon())
                .placeholder(R.mipmap.ic_default_image).into(holder.iv);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    class MyViewHolder extends RecyclerView.ViewHolder{
        ImageView iv;
        TextView tvNew,tvOld;
        public MyViewHolder(View itemView) {
            super(itemView);
            iv = (ImageView) itemView.findViewById(R.id.timed_promotions_item_iv);
            tvNew = (TextView) itemView.findViewById(R.id.timed_promotions_item_moneynew);
            tvOld = (TextView) itemView.findViewById(R.id.timed_promotions_item_moneyold);
        }
    }
}
